<template>
  <div class="login-container">
    <div class="login-container_header">
      <img
        src="http://127.0.0.1:3000/public/vue2admin/logo_admin1.png"
        class="header_logo"
      >
    </div>
    <div class="login-container_border" />
    <div class="login-container_main">
      <img
        src="http://127.0.0.1:3000/public/vue2admin/login.png"
        class="loginimg"
        alt="chahua"
      >
      <div class="logindoing">
        <div class="logindoing_top">
          <span>账号登录</span>
        </div>
        <div class="logindoing_mian">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户账号"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              >
                <span slot="prefix" class="svg-container">
                  <svg-icon icon-class="user" />
                </span>
              </el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="用户密码"
                name="password"
                tabindex="2"
              >
                <span
                  slot="prefix"
                  class="svg-container"
                  style="font-size: 12px"
                >
                  <svg-icon icon-class="password" />
                </span>
                <span slot="suffix" class="show-pwd" @click="showPwd">
                  <svg-icon
                    style="font-size: 14px"
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-input>
            </el-form-item>

            <el-form-item prop="identifyCode">
              <el-input
                ref="identifyCode"
                v-model="loginForm.identifyCode"
                placeholder="验证码"
                name="identifyCode"
                type="text"
                tabindex="3"
                @keyup.enter.native="handleLogin"
              >
                <i
                  slot="prefix"
                  style="font-size: 15px"
                  class="svg-container el-input__icon el-icon-success"
                />
                <div slot="suffix" class="get-code" @click="refreshCode()">
                  <s-identify :identify-code="identifyCode" />
                </div>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <div
          v-loading="loading"
          class="logindoing_buttton"
          @click="handleLogin"
        >
          <el-button
            :loading="loading"
            type="primary"
            style="width: 80%"
            @click.native.prevent.stop="handleLogin"
          >立即登录
          </el-button>
        </div>
      </div>
    </div>
    <div class="login-container_footer">
      <span>2020啊雄特开发个人管理系统 &copy; 2019-2080</span>
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import SIdentify from '@/components/SIdentify/index.vue'
import SvgIcon from '@/components/SvgIcon/index.vue'

export default {
  name: 'Login',
  components: { SvgIcon, SIdentify },
  data() {
    // 检验表单正则
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   // 这里账号做了分类验证[editor,admin]
      //   callback(new Error('未找到该账户名'))
      // } else {
      //   callback()
      // }
      if (!value) {
        // 这里账号做了分类验证[editor,admin]
        callback(new Error('账户名必填'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 6) {
        callback(new Error('用户密码错误'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value) {
        // 都转小写来验证
        if (
          this.identifyCode &&
          this.identifyCode.toLowerCase() === value.toLowerCase()
        ) {
          callback()
        } else {
          callback(new Error('验证码错误'))
        }
      } else {
        callback(new Error('验证码必填'))
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        identifyCode: '' // 输入表单验证码
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        identifyCode: [
          { required: true, trigger: 'blur', validator: validateCode }
        ]
      },
      loading: false, // 立即登录loading
      passwordType: 'password', // 控制显示隐藏密码
      redirect: undefined,
      identifyCodes: '0123456789abcdwerwshdjeJKDHRJHKOOPLMKQ', // 验证码绘制的随机数
      identifyCode: '' // 点击生成的验证码
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
        // console.log('this.redirect login=>', this.redirect)
      },
      immediate: true
    }
  },

  created() {
    // 初始化验证码
    this.refreshCode()
  },

  mounted() {
    // // 右键被禁用
    // document.oncontextmenu = function() {
    //   alert('右键被禁用')
    //   return false
    // }
    // // ctr +f12 禁用
    // document.onkeydown = function(e) {
    //   // console.log(e)
    //   if (e.code === 'F12' || e.ctrlKey) {
    //     // alert(''F12' || e.ctrlKey右键被禁用')
    //     return false
    //   }
    // }
  },

  methods: {
    // 显示隐藏密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 初始化验证码模块
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
      // console.log(' this.identifyCode', this.identifyCode)
    },
    randomNum(min, max) {
      max = max + 1
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 随机生成验证码字符串
    makeCode(data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += data[this.randomNum(0, data.length - 1)]
      }
    },
    // 立即登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((res) => {
              console.log('loginsss', res)
              // this.$router.push('/')
              this.$router.push({ path: this.redirect || '/' }, () => {})
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          // console.log('error submit!!')
          this.$message({
            message: '请填写完整登录信息',
            type: 'warning'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// $bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

@keyframes anScal {
  from {
    transform: scale(0.7);
  }

  to {
    transform: scale(1);
  }
}

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-image: url("../../assets/other_images/login_bg.png");
  background-position: 70% 40%;
  background-repeat: no-repeat;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  &_header {
    // background-color: #fff;
    height: 80px;
    width: 100%;
    display: flex;
    align-items: center;
    padding: 0 30px;

    .header_logo {
      width: 150px;
      height: auto;
    }
  }

  &_border {
    // height: 20px;
    width: 100%;
    background-color: #fff;
    border-top: 2px solid #f6f6f8;
  }

  &_main {
    flex: 1;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: space-evenly;

    .loginimg {
      width: 375px;
      animation: anScal 1s;
    }

    .logindoing {
      height: 400px;
      width: 316px;
      border-radius: 2px;
      padding: 16px;
      background-color: #fff;
      // filter: drop-shadow(0px 0.5px 4px #d4d4d4);
      box-shadow: 0 0 10px rgb(0, 0, 0, 0.2);
      display: flex;
      flex-direction: column;
      transition: box-shadow 0.5s;

      &:focus-within {
        box-shadow: 0 0 8px rgb(0, 0, 0, 0.5);
      }

      .logindoing_top {
        height: 60px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding: 0 8px;
        font-size: 20px;
        color: #000;
        font-weight: 450;
      }

      .logindoing_mian {
        flex: 1;
        width: 100%;
      }

      .logindoing_buttton {
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .get-code {
        margin-top: 1px;
      }
    }
  }

  &_footer {
    height: 80px;
    font-size: 12px;
    color: #333;
    display: flex;
    justify-content: center;
    align-items: flex-end;
  }

  .login-form {
    overflow: hidden;
  }

  .svg-container {
    height: 100%;
    color: $dark_gray;
    width: 30px;
    display: inline-block;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    padding: 0 8px;
  }
}
</style>
